﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Common;

namespace Business
{
    public partial class ProjectRegion
    {
        public static string CreateCode(long _projectID)
        {
            DataClassesDataContext db = null;
            try
            {
                db = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                string pID = _projectID.ToString();
                var query = db.ProjectRegions.Where(p => p.ProjectID == _projectID && p.RegionLevel == 3);
                if (query.Count() > 0)
                {
                    var last = query.OrderByDescending(o => o.CreatedDateTime).First();

                    string value = last.RegionCode.Split(new string[] { "R" + pID }, StringSplitOptions.None)[1];
                    int _value = int.Parse(value) + 1;
                    return "PR" + pID + _value.ToString("000");
                }
                else
                    return "PR" + pID + "001";
            }
            catch
            {
                return String.Empty;
            }
            finally
            {
                db = null;
            }
        }

        public static void GetCustomRegionForCombo(ref List<ComboItem> list, bool isAllOptions = false)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                var query = context.ProjectRegions.Where(p => p.RegionLevel == 3 && p.Status == 1);

                foreach (var item in query)
                {
                    ComboItem add = new ComboItem(item.ID, item.RegionCode, item.RegionName);
                    list.Add(add);
                }
                if (isAllOptions)
                    list.Insert(0, new ComboItem(0, "000000", "Tất cả"));
            }
            catch
            {
                list = null;
                return;
            }
            finally
            {
                context = null;
            }
        }

        public static void GetBaseRegionForCombo(ref List<ComboItem> list, bool isAllOptions = false)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                var query = context.ProjectRegions.Where(p => p.RegionLevel == 4 && p.Status == 1);

                foreach (var item in query)
                {
                    ComboItem add = new ComboItem(item.ID, item.RegionName);
                    list.Add(add);
                }
                if (isAllOptions)
                    list.Insert(0, new ComboItem("000000", "Tất cả"));
            }
            catch
            {
                list = null;
                return;
            }
            finally
            {
                context = null;
            }
        }

        public static Common.ERROR_CODE.ERROR_CODE_ITEMS_CHECK_DUPPLICATE CheckRegionNameExists(ref string messageError, string RegionName, long ProjectID)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                var query = context.ProjectRegions.Where(p => p.ProjectID == ProjectID && p.RegionName == RegionName && p.Status == 1);
                if (query.Count() == 0)
                {
                    messageError = "";
                    return Common.ERROR_CODE.ERROR_CODE_ITEMS_CHECK_DUPPLICATE.SUCCESS;
                }
                messageError = Common.EnumUtils.stringValueOf(Common.ERROR_CODE.ERROR_CODE_ITEMS_CHECK_DUPPLICATE.SYSTEM_ERROR);
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_CHECK_DUPPLICATE.SYSTEM_ERROR;
            }
            catch
            {
                messageError = Common.EnumUtils.stringValueOf(Common.ERROR_CODE.ERROR_CODE_ITEMS_CHECK_DUPPLICATE.SYSTEM_ERROR);
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_CHECK_DUPPLICATE.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }

        public static Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT GetCustomRegionByProjectID(ref string messageError, ref List<ProjectRegion> listPR, long projectID)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);
                var query = context.ProjectRegions.Where(dt => dt.ProjectID == projectID && dt.ParentCode != null && dt.Status == 1);

                listPR = query.ToList();
                messageError = "";
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SUCCESS;
            }
            catch
            {
                messageError = Common.EnumUtils.stringValueOf(Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SYSTEM_ERROR);
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_SELECT.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }

        // K00014
        public static Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW AddNew(ref string messageError, ref List<ProjectRegion> listPR, ref Common.UserInfo userInfo)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);

                List<ProjectRegion> newPR = new List<ProjectRegion>();
                foreach (var item in listPR)
                {
                    ProjectRegion prjRegion = new ProjectRegion();
                    prjRegion.ProjectID = item.ProjectID;
                    prjRegion.RegionCode = item.RegionCode;
                    prjRegion.RegionLevel = item.RegionLevel;
                    prjRegion.ParentCode = item.ParentCode;
                    prjRegion.RegionName = item.RegionName;
                    prjRegion.Status = (int)Common.STATUS_CODE.ROW_STATUS.ACTIVE;
                    prjRegion.CreatedBy = userInfo.UserID;
                    prjRegion.CreatedDateTime = DateTime.Now;
                    prjRegion.RowVersion = 1;

                    newPR.Add(prjRegion);

                    context.ProjectRegions.InsertOnSubmit(prjRegion);
                }

                context.SubmitChanges();

                listPR = newPR;

                return Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW.SUCCESS;
            }
            catch
            {
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_ADDNEW.SYSTEM_ERROR;
            }
            finally
            {
                context = null;
            }
        }

        public static Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE DisableProjectRegion(ref string messageError, ref List<ProjectRegion> listPR, long _prID, Common.UserInfo userinfo)
        {
            DataClassesDataContext db = null;
            try
            {
                db = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);

                long _ProjectID = listPR.First().ProjectID;

                ProjectRegion query = db.ProjectRegions.Single(p => p.ProjectID == _ProjectID && p.ID == _prID);

                query.Status = 0;

                listPR.Single(p => p.ProjectID == _ProjectID && p.ID == _prID).Status = 0;

                listPR = listPR.Where(p => p.Status == 1).ToList();

                db.SubmitChanges();
                messageError = "";
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE.SUCCESS;
            }
            catch
            {
                messageError = Common.EnumUtils.stringValueOf(Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE.SYSTEM_ERROR);
                return Common.ERROR_CODE.ERROR_CODE_ITEMS_UPDATE.SYSTEM_ERROR;
            }
            finally
            {
                db = null;
            }
        }

        public static void GetProjectRegionByProjectID(ref List<ProjectRegion> list, long projectID, bool getAll = false)
        {
            DataClassesDataContext context = null;
            try
            {
                context = new DataClassesDataContext(Common.Global.GetConnectionInfo().ConnectionStringSQL);

                if (getAll)
                {
                    var query = context.ProjectRegions.Where(dt => dt.ProjectID == projectID);
                    list = query.ToList();
                }
                else
                {
                    var query = context.ProjectRegions.Where(dt => dt.ProjectID == projectID && dt.Status == 1);
                    list = query.ToList();
                }
            }
            catch
            {
                list = null;
                return;
            }
            finally
            {
                context = null;
            }
        }

        public class SimpleProjectRegion
        {
            public long ID { get; set; }
            public long ProjectID { get; set; }
            public string RegionCode { get; set; }
        }
    }
}
